/*
 * @Author: wfl
 * @LastEditors: wfl
 * @description: 
 * @updateInfo: 本次更新内容：
 * @Date: 2021-12-08 15:07:40
 * @LastEditTime: 2021-12-09 14:35:03
 */
import * as Cesium from 'cesium';
// import createPropertyDescriptor from "cesium/Source/DataSources/createPropertyDescriptor.js";
// import Property from "cesium/Source/DataSources/Property.js";

// 着色器代码
// "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
// {\n\
//     czm_material material = czm_getDefaultMaterial(materialInput);\n\
//     vec2 st = materialInput.st;\n\
//     vec4 colorImage = texture2D(image, vec2(fract(-(st.t + time)), st.t));\n\
//     material.alpha = colorImage.a * color.a;\n\
//     material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
//     return material;\n\
// }"

// 着色器代码
// "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
// {\n\
//     czm_material material = czm_getDefaultMaterial(materialInput);\n\
//     vec2 st = materialInput.st;\n\
//     vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t));\n\
//     material.alpha = colorImage.a * color.a;\n\
//     material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
//     return material;\n\
// }"

// "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
// {\n\
//     czm_material material = czm_getDefaultMaterial(materialInput);\n\
//     vec2 st = materialInput.st;\n\
//     vec4 colorImage = texture2D(image, vec2(fract(-(st.s + time)), st.t));\n\
//     material.alpha = colorImage.a * color.a;\n\
//     material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
//     return material;\n\
// }";

// "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
// {\n\
//     czm_material material = czm_getDefaultMaterial(materialInput);\n\
//     vec2 st = materialInput.st;\n\
//     vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
//     material.alpha = colorImage.a * color.a;\n\
//     material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
//     return material;\n\
// }"


const source = "czm_material czm_getMaterial(czm_materialInput materialInput)\n" +
  "{\n" +
  "czm_material material = czm_getDefaultMaterial(materialInput);\n" +
  "material.diffuse = 1.5 * color.rgb;\n" +
  "vec2 st = materialInput.st;\n" +
  "float dis = distance(st, vec2(0.5, 0.5));\n" +
  "float per = fract(st.t - time);\n" +
  "material.alpha = color.a * dis / per / 1.0;\n" +
  "return material;\n" +
  "}";

// "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
// {\n\
//     czm_material material = czm_getDefaultMaterial(materialInput);\n\
//     vec2 st = materialInput.st;\n\
//     vec4 colorImage = texture2D(image, vec2(fract(-(st.t + time)), st.t));\n\
//     material.alpha = colorImage.a * color.a;\n\
//     material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
//     return material;\n\
// }"

function AnimationPointMaterialProperty(color, duration) {
  // Cesium.PolylineTrailLinkMaterialProperty = AnimationPointMaterialProperty;
  Cesium.Material.AnimationPointMaterialType = 'AnimationPoint';
  Cesium.Material.AnimationPointMaterialSource = source;
  Cesium.Material._materialCache.addMaterial(Cesium.Material.AnimationPointMaterialType, {
    fabric: {
      type: Cesium.Material.AnimationPointMaterialType,
      uniforms: {
        color: new Cesium.Color(1.0, 0.0, 0.0, 1),
        time: 0
      },
      source: Cesium.Material.AnimationPointMaterialSource
    },
    translucent: function (material) {
      return true;
    }
  })
  this._definitionChanged = new Cesium.Event();
  this._color = undefined;
  this._colorSubscription = undefined;
  this.color = color;
  this.duration = duration;
  this._time = (new Date()).getTime();
}

Object.defineProperties(AnimationPointMaterialProperty.prototype, {
  isConstant: {
    get: function () {
      return Cesium.Property.isConstant(this._color);
    },
  },

  definitionChanged: {
    get: function () {
      return this._definitionChanged;
    },
  },

  color: Cesium.createPropertyDescriptor("color"),
});

AnimationPointMaterialProperty.prototype.getType = function () {
  return "AnimationPoint";
};

AnimationPointMaterialProperty.prototype.getValue = function (time, result) {
  if (!Cesium.defined(result)) {
    result = {};
  }
  result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
  result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
};

AnimationPointMaterialProperty.prototype.equals = function (other) {
  return (
    this === other ||
    (other instanceof AnimationPointMaterialProperty &&
      Cesium.Property.equals(this._color, other._color))
  );
};
export default AnimationPointMaterialProperty;